package com.yuehuanghun.admin.core.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.yuehuanghun.admin.core.config.annotation.mybatislilu.CreateBy;
import com.yuehuanghun.admin.core.config.annotation.mybatislilu.CreateTime;
import com.yuehuanghun.admin.core.config.annotation.mybatislilu.UpdateBy;
import com.yuehuanghun.mybatis.milu.annotation.AttributeOptions;
import com.yuehuanghun.mybatis.milu.annotation.ExampleQuery;
import com.yuehuanghun.mybatis.milu.annotation.ExampleQuery.MatchType;
import com.yuehuanghun.mybatis.milu.annotation.alias.attr.UpdateTime;
import com.yuehuanghun.mybatis.milu.annotation.alias.id.SnowflakeId;

import lombok.Data;

/**
 * 菜单权限表 sys_menu
 * 
 * @author yadmin
 */
@Data
@Entity
public class SysMenu implements Serializable {
	private static final long serialVersionUID = 1L;

	/** 菜单ID */
	@SnowflakeId
	@JsonFormat(shape = Shape.STRING)
	private Long menuId;

	/** 菜单名称 */
	@NotBlank(message = "菜单名称不能为空")
	@Size(min = 0, max = 50, message = "菜单名称长度不能超过50个字符")
	@AttributeOptions(exampleQuery = @ExampleQuery(matchType = MatchType.CONTAIN))
	private String menuName;

	/** 父菜单名称 */
	@Transient
	private String parentName;

	/** 父菜单ID */
	@JsonFormat(shape = Shape.STRING)
	private Long parentId;

	/** 显示顺序 */
	@NotBlank(message = "显示顺序不能为空")
	private String orderNum;

	/** 路由地址 */
	@Size(min = 0, max = 200, message = "路由地址不能超过200个字符")
	private String path;

	/** 组件路径 */
	@Size(min = 0, max = 200, message = "组件路径不能超过255个字符")
	private String component;

	/** 是否为外链（0是 1否） */
	private String isFrame;

	/** 是否缓存（0缓存 1不缓存） */
	private String isCache;

	/** 类型（M目录 C菜单 F按钮） */
	@NotBlank(message = "菜单类型不能为空")
	private String menuType;

	/** 显示状态（0显示 1隐藏） */
	private String visible;

	/** 菜单状态（0显示 1隐藏） */
	private String status;

	/** 权限字符串 */
	@Size(min = 0, max = 100, message = "权限标识长度不能超过100个字符")
	private String perms;

	/** 菜单图标 */
	private String icon;

	/** 创建者 */
	@CreateBy
	private String createBy;

	/** 创建时间 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@CreateTime
	private Date createTime;

	/** 更新者 */
	@UpdateBy
	private String updateBy;

	/** 更新时间 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@UpdateTime
	private Date updateTime;

	/** 备注 */
	private String remark;
	
	@ManyToMany
	@JoinTable(name = "sys_role_menu", joinColumns = @JoinColumn(name = "menu_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	private List<SysRole> roles;

	/** 子菜单 */
	@Transient
	private List<SysMenu> children = new ArrayList<SysMenu>();

	/** 请求参数 */
	@Transient
    private Map<String, Object> params;
	
	public Map<String, Object> getParams() {
		return params == null ? (params = new HashMap<>()) : params;
	}
}
